﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class addOrEditMiscellaneousCharges : Form
    {
        TextBox txt;
        String val;
        miscellaneousCharges parent;

        public addOrEditMiscellaneousCharges(miscellaneousCharges parent)
        {
            this.parent = parent;
            InitializeComponent();
            txt = (TextBox)amount.Controls[1];
            txt.TextChanged += new EventHandler(txt_TextChanged);
            txt.Validating += new CancelEventHandler(txt_Validating);
            val = txt.Text;
            amount.Value = parent.getAmount();
            startDatePicker.Value = DateTime.Parse(parent.getCurrentDate());
            endDatePicker.Value = DateTime.Parse(parent.getCurrentEnd());
        }

        private void miscellaneousChargesSaveButton_Click(object sender, EventArgs e)
        {
            MiscChargePeriod miscCharge = new MiscChargePeriod();
            DateTime date = DateTime.Parse(parent.getCurrentDate());
            miscCharge.setStartDate(date.ToString("yyyy-MM-dd"));
            miscCharge.setMiscCharge((double)amount.Value);
            bool success = new MiscChargeController().editMiscCharge(miscCharge);
            if (success)
            {
                parent.initializeContent();
                this.Close();
            }
        }

        private void miscellaneousChargesCancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txt_TextChanged(object sender, EventArgs e)
        {
            if (txt.Focused)
                val = txt.Text;
        }

        private void txt_Validating(object sender, CancelEventArgs e)
        {
            if (multipleDecPoints(val))
            {
                MessageBox.Show("Invalid input; too many decimal points.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
                if (val.Equals(".")||val.Contains("-"))
                {
                    MessageBox.Show("Invalid input.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    double check = Double.Parse(val);
                    if (check < (double)amount.Minimum || check > (double)amount.Maximum)
                        MessageBox.Show("Invalid input; out of range.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
        }

        private bool multipleDecPoints(String num)
        {
            int nPoints = 0;
            int i = 0;
            while (nPoints < 2 && i < num.Length)
            {
                if (num[i] == '.')
                    nPoints++;
                i++;
            }
            if (nPoints < 2)
                return false;
            else
                return true;
        }

    }
}
